余烬缀记

Debian 编译安装 Nginx

edited on:

# Debian 编译安装 Nginx

本文参考了https://blog.shmily.one/linux/debian-compile-install-nginx.html,在写这篇文章前基本参照着这篇文章配置的

如果手动需要重装系统可参考https://lala.im/7233.html这篇文章

# 第一步 下载源代码

官网:https://nginx.org/en/download.html

本文编写时版本:1.18.0

进入/usr/local/src,软件都安装在/usr/local,源代码放在/usr/local/src

使用wget下载包:wget https://nginx.org/download/nginx-1.17.8.tar.gz

解压文件:tar xf nginx-1.17.8.tar.gz

进入解压的 nginx 文件夹

创建一个给 nginx 使用的用户(不建议使用 root 或者管理者的账号)

# 添加用户
sudo useradd -s /usr/sbin/nologin nginx -M
# 或者将nginx用户添加到管理者的用户组
sudo useradd -g group -s /usr/sbin/nologin nginx -M

# 第二步 编译

本文推荐下载的所有包都放在/var/tmp/下面

解压至/usr/local/src下面然后进行编译,以名称 + 版本号命名,下方的 nginx 编译也是在该文件夹下面

# 依赖项

prefix可不填,默认为:/usr/local/nginx

sbin-path可不填,默认为:${prefix}/sbin

conf-path同上,error-loghttp-log可不填,默认为:/var/log/nginx

ssl 依赖 openssl, 地址:https://www.openssl.org/source/ 目前版本:1.1.1d

[可选] 使用 libressl 替代,地址:http://ftp.openbsd.org/pub/OpenBSD/LibreSSL/ 目前版本:3.3.1

gzip 依赖 zlib,地址:http://www.zlib.net/fossils/ 目前版本:1.2.11

rewrite 依赖 pcre, 地址:https://ftp.pcre.org/pub/pcre/ 目前版本:8.43

以上三个包在解压后均使用以下命令进行编译

./configure
make
make install

# 遇到的问题

# 生成编译配置

参数意义及以其他参数文档地址:http://nginx.org/en/docs/configure.html

./configure \
	--user=nginx \
	--group=nginx \
    --prefix=/usr/local/src/nginx-1.18.0 \
    --sbin-path=/usr/local/sbin/nginx \
	--conf-path=/usr/local/etc/nginx/nginx.conf \
	--error-log-path=/var/log/nginx/error.log \
	--http-log-path=/var/log/nginx/access.log \
	--with-stream \
	--with-http_stub_status_module \
	--with-http_v2_module \
	--with-http_ssl_module \
	--with-http_gzip_static_module \
	--with-http_realip_module \
	--with-pcre="pcre路径,对于自动安装在可不填" \
	--with-pcre-jit \
	--with-openssl="openssl或路径libressl路径" \
	--with-http_sub_module

编译

make
make install

测试有没有错误

/usr/local/sbin/nginx -t

查看 Nginx 版本和详细资料

/usr/local/sbin/nginx -V

查看端口占用

 sudo lsof -i:80
 # 如果被暂用了,使用下面的命令结束端口占用程序
 sudo kill pid

# 第三步 配置启动脚本

下载脚本https://gist.github.com/piecego/dfc0278936a3aaeb8c144a453735091e/etc/init.d命名为nginx

wget -O /etc/init.d/nginx https://gist.githubusercontent.com/piecego/dfc0278936a3aaeb8c144a453735091e/raw/d8973351e846b7373170d3b63813977f14a9b552/nginx-setup-script-for-debian.sh

给予执行权限

chmod +x /etc/init.d/nginx

重载 systemd

systemctl daemon-reload

启动 nginx

sudo systemctl start nginx

查看 nginx 状态

sudo systemctl status nginx

停止 nginx

sudo systemctl stop nginx

重启 nginx

sudo systemctl restart nginx

重新载入 nginx 的 config 档

sudo systemctl reload nginx

设置开机启动

update-rc.d -f nginx defaults
# 或者
sudo systemctl enable nginx